package com.cskaoyan.service;

import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.domain.MarketFeedback;
import com.cskaoyan.domain.MarketFeedbackExample;
import com.cskaoyan.domain.MarketUser;
import com.cskaoyan.mapper.MarketFeedbackMapper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class FeedbackServiceImpl implements FeedbackService {
    @Autowired
    MarketFeedbackMapper feedbackMapper;
    @Override
    @Transactional
    public BaseRespVo adviceSubmit(Map map, Integer userId) {
        String content = (String) map.get("content");
        String feedType = (String) map.get("feedType");
        Boolean hasPicture = (Boolean) map.get("hasPicture");
        String mobile = (String) map.get("mobile");
        List<String> picUrls= (List<String>) map.get("picUrls");
        MarketFeedback feedback = new MarketFeedback();
        Subject subject = SecurityUtils.getSubject();
        feedback.setUserId(userId);
        MarketUser principal = (MarketUser) subject.getPrincipal();
        feedback.setUsername(principal.getUsername());
        if (mobile.length()== 11) {
            feedback.setMobile(mobile);
        } else {
            return BaseRespVo.fail("手机位数不对！");
        }
        feedback.setFeedType(feedType);
        feedback.setContent(content);
        feedback.setHasPicture(hasPicture);
        feedback.setPicUrls(picUrls.toArray(new String[picUrls.size()]));
        feedback.setAddTime(LocalDateTime.now());
        feedback.setUpdateTime(LocalDateTime.now());
        feedback.setDeleted(false);
        feedbackMapper.insertSelective(feedback);
        return BaseRespVo.ok();
    }
}
